package cn.core.dirs.rest;

import cn.core.dirs.ao.FileUploadAO;
import cn.core.dirs.service.intf.IFileUploadService;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 文件上传 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("dirs")
public class FileUploadController extends CoreController {

  @Resource
  private IFileUploadService fileUploadService;

  /**
   * 文件上传
   * @param fileUploadAO 参数
   * @return 返回
   */
  @PostMapping("fileUpload")
  public Res<String> fileUpload(FileUploadAO fileUploadAO) throws Exception {
    Res<String> res = new Res<>("文件上传成功！");
    try {
      if (CoreTool.isNullEo(fileUploadAO) || CoreTool.isNullStr(fileUploadAO.getFileModule())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(fileUploadService.fileUpload(fileUploadAO));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
